package no.ntnu.item.ttm4115.termproject.teacheragent.gui;

import java.io.File;

import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileSystemView;

import no.ntnu.item.ttm4115.termproject.StartGameFromGUI;
import no.ntnu.item.ttm4115.termproject.StartUpGame;
import no.ntnu.item.ttm4115.termproject.extrasagent.Game;
import no.ntnu.item.ttm4115.termproject.extrasagent.Treasures;
import no.tellu.common.actorframe.ActorSM;
import no.tellu.common.javaframe.ActorAddress;

/*
 * LoadGame.java
 *
 * Created on 15. juli 2008, 13:53
 */



/**
 *
 * @author  bruker
 */
public class LoadGame extends javax.swing.JPanel {
	private TeacherGUI gui;
	private ActorSM asm;
	private String file;
	private Game game;
	public LoadGame(TeacherGUI gui,ActorSM asm){
		gui.setStartPanel(this);
		this.gui = gui;
		this.asm = asm;
		initComponents();
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this.gui.getContentPane());
       this.gui.getContentPane().setLayout(layout);
       layout.setHorizontalGroup(
           layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
           .addComponent(this, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
       );
       layout.setVerticalGroup(
           layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
           .addComponent(this, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
       );
	}
    
    private void initComponents() {

        selectGameButton = new javax.swing.JButton();
        gameNameLabel = new javax.swing.JLabel();
        treasuresLabel = new javax.swing.JLabel();
        groupsLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        groupsList = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        treasuresList = new javax.swing.JList();
        actualGameNameLabel = new javax.swing.JLabel();
        loadGameButton = new javax.swing.JButton();
        startGameButton = new javax.swing.JButton();
        treasuresListModel = new javax.swing.DefaultListModel();
        groupsListModel = new javax.swing.DefaultListModel();
        
        
        selectGameButton.setText("Select Game");
        selectGameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectGameButtonActionPerformed(evt);
            }
        });

        gameNameLabel.setText("Game Name");

        treasuresLabel.setText("Treasures");

        groupsLabel.setText("Groups");
        
        groupsList.setModel(getGroupsListModel());
        jScrollPane1.setViewportView(groupsList);
        
        
        treasuresList.setModel(getTreasuresListModel());
        jScrollPane2.setViewportView(treasuresList);

        actualGameNameLabel.setText("this is  the game name");

        loadGameButton.setText("Load Game");
        loadGameButton.addActionListener(new java.awt.event.ActionListener() {
        	public void actionPerformed(java.awt.event.ActionEvent evt) {
        		loadGameButtonActionPerformed(evt);
        	}
        });

        startGameButton.setText("Start Game");
        startGameButton.setEnabled(false);
        startGameButton.addActionListener(new java.awt.event.ActionListener() {
        	public void actionPerformed(java.awt.event.ActionEvent evt) {
        		startGameButtonActionPerformed(evt);
        	}
        });
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(selectGameButton)
                .addGap(129, 129, 129)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(gameNameLabel)
                        .addGap(18, 18, 18)
                        .addComponent(actualGameNameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(treasuresLabel)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                            .addComponent(loadGameButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(55, 55, 55)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(groupsLabel)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                            .addComponent(startGameButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(42, 42, 42)))
                .addContainerGap(75, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(gameNameLabel)
                            .addComponent(actualGameNameLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(treasuresLabel)
                            .addComponent(selectGameButton)))
                    .addComponent(groupsLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE))
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loadGameButton)
                    .addComponent(startGameButton))
                .addContainerGap(141, Short.MAX_VALUE))
        );
    }// </editor-fold>

    private void selectGameButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	JFileChooser fc = new JFileChooser(".");
    	fc.setCurrentDirectory(new File("c:/ttm4115config/"));
    	int result = fc.showOpenDialog(null);
    	if(result == JFileChooser.APPROVE_OPTION){
    		int response = JOptionPane.showConfirmDialog(null, "You have selected:"+newline+fc.getSelectedFile().toString()+newline+"Is this the correct file?", 
       			 "Confirm Game selection",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
   	            if (response == JOptionPane.NO_OPTION) {
   	              
   	            } else if (response == JOptionPane.YES_OPTION) {
   	            	//asm.sendMessage(new ReleaseThisImAgent(), asm.getContext().getParentAddress());
   	            	file = fc.getSelectedFile().toString();
   	            	paintGameInfo(file);
   	            } else if (response == JOptionPane.CLOSED_OPTION) {
   	            	
   	            }
    	}else{
    		System.out.println("Cancel trykket");
    	}
    }
    private void loadGameButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	asm.sendMessage(new StartGameFromGUI(this.game.getName()),new ActorAddress("/configurationAgent","ConfigurationAgent"));
    	startGameButton.setEnabled(true);
    }
    private void startGameButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	asm.sendMessage(new StartUpGame(),gui.getHashedGames().get(this.game.getName()));
    }
    private void paintGameInfo(String file){
    	String[] split1 = file.split("\\\\");
    	String[] split2 = split1[split1.length-1].split("\\.");
    	this.game = gui.getGames().get(split2[0]);
      	actualGameNameLabel.setText(this.game.getName());
      	fillGroupsList(getGroupsListModel());
      	fillTreasuresList(getTreasuresListModel());   	
    }
    private DefaultListModel  getTreasuresListModel(){
    	return treasuresListModel;
    }
    private DefaultListModel getGroupsListModel(){
    	return groupsListModel;
    }
    private void fillGroupsList(DefaultListModel model) { 
    	model.removeAllElements();
    	for (int i = 0; i < game.getGroups().size(); i++) {
			model.addElement(game.getGroups().elementAt(i).getGroupName());
		}
    }
    private void fillTreasuresList(DefaultListModel model) {
    	model.removeAllElements();
    	for (int i = 0; i < game.getTreasures().size(); i++) {
    		model.addElement(((Treasures) game.getTreasures().elementAt(i)).getName());
		}
    }

    private javax.swing.JLabel actualGameNameLabel;
    private javax.swing.JLabel gameNameLabel;
    private javax.swing.JLabel groupsLabel;
    private javax.swing.JList groupsList;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton loadGameButton;
    private javax.swing.JButton selectGameButton;
    private javax.swing.JButton startGameButton;
    private javax.swing.JLabel treasuresLabel;
    private javax.swing.JList treasuresList;
    private final static String newline = "\n";
    private javax.swing.DefaultListModel treasuresListModel;
    private javax.swing.DefaultListModel groupsListModel;
    

}

